﻿using System;
using System.Data;
using System.Data.Common;
using FirebirdSql.Data.FirebirdClient;

namespace BingDesktopChanger
{
    /// <summary>
    /// Wrappers access to firebird database
    /// </summary>
    internal class DataContext
    {
        public virtual int ExecuteSentence(string sentence)
        {
            try
            {
                int result = int.MinValue;
                this.CreateConnection();
                using (var connection = this.CreateConnection())
                using (var cmd = this.CreateCommand(connection, sentence))
                    result = cmd.ExecuteNonQuery();
                return result;
            }
            catch (Exception ex)
            {
                Helper.LogException(ex);
                return -1;
            }
        }

        public virtual object ExecuteScalar(string sentence)
        {
            return ExecuteScalar(sentence, null);
        }
        public virtual object ExecuteScalar(string sentence, params DbParameter[] parameters)
        {
            try
            {
                object result = null;
                using (var connection = this.CreateConnection())
                {
                    using (var cmd = this.CreateCommand(connection, sentence))
                    {
                        if (parameters != null && parameters.Length > 0)
                            cmd.Parameters.AddRange(parameters);
                        result = cmd.ExecuteScalar();
                    }

                }
                return result;
            }
            catch (Exception ex)
            {
                Helper.LogException(ex);
                return null;
            }
        }

        public virtual IDataReader ExecuteDataReader(string query)
        {
            try
            {
                var connection = this.CreateConnection();
                using (var cmd = this.CreateCommand(connection, query))
                    return cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                Helper.LogException(ex);
                return null;
            }
        }

        public virtual DataTable ExecuteDataTable(string query)
        {
            try
            {
                DataTable t = new DataTable();
                using (var connection = this.CreateConnection())
                using (var cmd = this.CreateCommand(connection, query))
                    t.Load(cmd.ExecuteReader());
                return t;
            }
            catch (Exception ex)
            {
                Helper.LogException(ex);
                return null;
            }
        }


        private DbCommand CreateCommand(DbConnection connection, string sentence)
        {
            var command = connection.CreateCommand();
            command.CommandText = sentence;
            command.CommandType = CommandType.Text;
            return command;
        }

        private DbConnection CreateConnection()
        {
            //var connection = new FbConnection(this.connectionString);
            FbConnectionStringBuilder csb = new FbConnectionStringBuilder();
            csb.Database = "BingDesktopChanger.fdb";
            csb.ServerType = FbServerType.Embedded;
            csb.UserID = "sysdba";
            FbConnection connection = new FbConnection(csb.ToString());
            connection.Open();
            return connection;
        }
    }
}
